package com.bdmc.smokesensor.services;

import java.util.*;

import com.bdmc.smokesensor.dao.Smk_MaintenanceJPA;
import com.bdmc.smokesensor.entity.Smk_Maintenance;
import com.bdmc.smokesensor.iservices.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class Smk_MaintenanceService extends BaseService<Smk_Maintenance> implements ISmk_MaintenanceService {

    @Autowired
    public Smk_MaintenanceJPA _mainJPA;

    @Override
    public List<Smk_Maintenance> findAll() {
        String sql = "select a.* from Smk_Maintenance a";
        List<Smk_Maintenance> list = GetListBySql(sql, Smk_Maintenance.class);
        return list;
    }

    @Override
    public Map<String, Object> findBySqlPageable(int page, int size) {
        String sql = "select a.* from Smk_Maintenance a";
        List<Smk_Maintenance> p = GetListBySqlPageable(sql, Smk_Maintenance.class, page, size);
        int total = GetCountBySql(sql);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", total);
        map.put("data", p);
        return map;
    }
    @Override
    public Smk_Maintenance findOne(Example<Smk_Maintenance> example) {
        return _mainJPA.findOne(example).orElse(null);
    }

    @Override
    public Smk_Maintenance findOneById(int id) {
        return _mainJPA.findById(id).orElse(null);
    }

    @Override
    public Smk_Maintenance save(Smk_Maintenance d) {
        if ( d.getId() < 1)
            d.setJoinDate(new Date());
        else
            d.setUpdateDate(new Date());
        return _mainJPA.save(d);
    }

    @Override
    public void deleteById(int id) {
        _mainJPA.deleteById(id);
    }

    @Override
    public void updateDelete(int id) {
        Smk_Maintenance d = _mainJPA.findById(id).orElse(null);
        d.setDel(true);
        _mainJPA.save(d);
    }

    @Override
    public Map<String, Object> getCurrentMainten(int dep_id,String mainName, Pageable pageable) {
        Page<Smk_Maintenance> page = _mainJPA.getCurrentMainten(dep_id,","+dep_id+",",mainName, pageable);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", page.getTotalElements());
        map.put("data", page.getContent());
        return map;
	}

    @Override
    public List<Map<String, Object>> getCurrentCombo(int dep_id) {
        return _mainJPA.getCurrentCombo(dep_id, ","+dep_id+",");
    }

}